
var iaz_preserved_elements = [];
var iaz_preserved_zindexes = [];

var QWebSearchText = QWebControl.extend('QSearchText', {

	_lastTextValue : "",

	load : function ()
	{
		var ref_this = this;

		// jQuery(this.domElement).css("position", "relative");
		/*
		jQuery("input", this.domElement).change(
				function ()
				{
					ref_this.searchTextChange(this);
				}
			);
			/*/
		// // 
		if (jQuery.browser.msie)
		{
			var browser_version = parseFloat(jQuery.browser.version);
			
			if (browser_version < 8.0)
			{
				// alert(browser_version);
				var z_index = 100000;
				jQuery(this.domElement).each(
					function ()
					{
						// alert("div: " + jQuery(this).attr("class") + " | z-index: " + jQuery(this).css("z-index"));
						jQuery(this).css("z-index", "" + z_index);
						z_index -= 10;
					});

				jQuery("div", this.domElement).each(
					function ()
					{
						// alert("div: " + jQuery(this).attr("class") + " | z-index: " + jQuery(this).css("z-index"));
						jQuery(this).css("z-index", "" + z_index);
						z_index -= 10;
					});
			}
		}

		jQuery("input", this.domElement).keyup(
			function ()
			{
				ref_this.refreshResults();
			});

		jQuery("input", this.domElement).focus(
			function ()
			{
				ref_this.refreshResults();
			});
		// alert(GetQWebPage().MaxZIndex);
		jQuery(document.body).bind("click",
			function (e)
			{
				if (e.target)
				{
					// make sure this.domElement is not his parent
					var parent = e.target;
					do
					{
						if (parent == ref_this.domElement)
							return;
						if (jQuery(parent).parent().length > 0)
							parent = jQuery(parent).parent()[0];
						else
							break;
					}
					while (true);

					// jQuery(".qSearchTextResults", ref_this.domElement).hide();
					ref_this.textResultsHide();
				}
			});

		// jQuery(".qSearchTextResultsItemDisabled", this.domElement).live("click", function (e) {alert("disabled");});
	},

	clearValue : function ()
	{
		var old_value = jQuery("input", this.domElement).val();
		var new_value = "";
		jQuery("input", this.domElement).val( new_value );
		
		// jQuery(".qSearchTextResults", this.domElement).hide();
		this.textResultsHide();

		// ref_this.searchTextChange(jQuery("input", ref_this.domElement).val());
		this.trigger({type: "ev_searchTextChange", searchText : new_value, searchTextOld : old_value});
	},

	refreshResults : function ()
	{
		// alert("refreshResults");
		var the_val = jQuery("input", this.domElement).val();
		var old_val = this._lastTextValue;

		this._lastTextValue = the_val;

		this.triggerEvent("refreshResults", {searchText : the_val},
				this, "afterRefreshResults");
		if ((the_val.length == 0) && (old_val != the_val))
		{
			// alert("clear :" + the_val + "|" + old_val+"]");
			this.trigger({type: "ev_searchTextChange", searchText : the_val, searchTextOld : old_val});
		}
	},

	getDataForCaption : function (caption)
	{
		var results = this.getJsParam("results");
		if (!results)
			return null;

		for (var i = 0; i < results.length; i++)
			if (results[i]["caption"] == caption)
				return results[i];

		return null;
	},

	getDataFor : function (key, value)
	{
		var results = this.getJsParam("results");
		if (!results)
			return null;

		for (var i = 0; i < results.length; i++)
			if (results[i][key] == value)
				return results[i];

		return null;
	},
	
	textResultsHide : function()
	{
		jQuery(".qSearchTextResults", this.domElement).hide();
		// jQuery(".qSearchTextResults", this.domElement).parent().css("z-index", "0");
		jQuery(".qSearchTextResults", this.domElement).css("z-index", "0");
		// jQuery(".qSearchTextResults > div", this.domElement).css("z-index", "0");
	},
	
	textResultsShow : function()
	{
		// jQuery(".qSearchTextResults", this.domElement).parent().css("z-index", "" + (GetQWebPage().MaxZIndex - 1));
		jQuery(".qSearchTextResults", this.domElement).css("z-index", "" + GetQWebPage().MaxZIndex);
		// this.zIndexFixIE(".qSearchTextResults > div", "" + GetQWebPage().MaxZIndex);
		// jQuery(".qSearchTextResults > div", this.domElement).css("z-index", "" + (GetQWebPage().MaxZIndex + 1));
		jQuery(".qSearchTextResults", this.domElement).show();
	},

	afterRefreshResults : function (msg)
	{
		// alert("afterRefreshResults");
		// alert("afterRefreshResults");
		// alert(jQuery(".qSearchTextResultsItem", caller.domElement).length);
		var ref_this = this;

		var items_list = jQuery(".qSearchTextResultsItem", this.domElement);
		if (items_list.length > 0)
		{
			this.textResultsShow();

			items_list.click(
					function()
					{
						if (jQuery(this).hasClass("qSearchTextResultsItemDisabled"))
							return;
						// alert(this);'
						var old_value = jQuery("input", ref_this.domElement).val();
						var new_value = jQuery(".qSearchTextResultsItemValue", this).text();
						
						// alert(jQuery(".qSearchTextResultsItemValue", this)[0].innerText);
						
						jQuery("input", ref_this.domElement).val( new_value );
						// ref_this._user_select = true;
						// jQuery(".qSearchTextResults", ref_this.domElement).hide();
						ref_this.textResultsHide();

						ref_this._lastTextValue = new_value;

						// ref_this.searchTextChange(jQuery("input", ref_this.domElement).val());
						// if (old_value != new_value)
						ref_this.trigger({type: "ev_searchTextChange", searchText : new_value, searchTextOld : old_value});
					}
				);
		}
		else
		{
			this.textResultsHide();
		}
	},
	
	zIndexFixIE : function (element, zindex, context_id)
	{
		// only for IE
		if (!jQuery.browser.msie)
			return;
		// default values
		if (undefined == zindex)
			zindex = 1;

		var context = (undefined == context_id ? jQuery(context_id) : jQuery(this.domElement));
		var element = jQuery(element);

		// undo past zIndexFixIE()
		for (var i = iaz_preserved_elements.length-1; i >= 0; i--)
			iaz_preserved_elements[i].css('z-index', iaz_preserved_zindexes[i]);

		iaz_preserved_elements = [];
		iaz_preserved_zindexes = [];

		// find relative-positioned parents of element within context
		element.parents().each(
			function()
			{
				// alert(this.tagName);
				if ('relative' == jQuery(this).css('position'))
				{
					// preserve ancestor's current z-index
					// alert(this.tagName);
					iaz_preserved_elements.push(jQuery(this));
					iaz_preserved_zindexes.push(jQuery(this).css('z-index') );

					// apply z-index to ancestor
					jQuery(this).css('z-index', zindex);
					// return false;
				}

				// if context found, stop looping
				if (this == context[0])
				{
					// alert("breaking out : " + context[0].tagName);
					return false;
				}
			}
		);
	}

});
